var upload={
	error:function(msg){
		common_ops.alert(msg);
	},
	success:function(image_key){
		$html='<img src="'+common_ops.buildPicUrl('brand',image_key)+'">'+
		'<span class="fa fa-times-circle del del_image" data='+image_key+'><i></i></span>';
		if($(".upload_pic_wrap .pic-each").size()>0){
			$(".upload_pic_wrap .pic-each").html($html);
		}else{
			$(".upload_pic_wrap").append('<span class="pic-each">'+$html+'</span>');
		}
		brand_img_ops.delete_img();
	}	
};
var brand_img_ops={
	init:function(){
		this.eventBind();
		this.delete_img();
	},
	eventBind:function(){
		$(".set_pic").click(function(){
			$("#myModal").modal('show');
		});
		$("input[name=pic]").change(function(){
			$(".upload_pic_wrap").submit();
		});
		$("#save-pic").click(function(){
			var btn_target=$(this);
			if(btn_target.hasClass("disabled")){
				common_ops.alert('请不要重复点击');
			}

			if($(".upload_pic_wrap .pic-each").size()<1){
				common_ops.alert("没有上传品牌图片");
				return;
			}
			btn_target.addClass("disabled");
			$.ajax({
				url:common_ops.buildWebUrl('/brand/set-img'),
				type:"POST",
				data:{
					image_key:$('.del_image').attr('data')
				},
				dataType:"json",
				success:function(res){
					btn_target.removeClass("disabled");
					callback=null;
					if(res.code==200){
						callback=function(){
						window.location.href=window.location.href;
						};
					}
					common_ops.alert(res.msg,callback);
				},
			});
		});
		$(".remove").click(function(){
			
			var id=$(".remove").attr('data');
			var btn_target=$(this);
			if(btn_target.hasClass("disabled")){
				common_ops.alert('请不要重复点击');
			}
			btn_target.addClass("disabled");
			$.ajax({
				url:common_ops.buildWebUrl('/brand/del-img'),
				type:"POST",
				data:{
					id:id
				},
				dataType:"json",
				success:function(res){
					btn_target.removeClass("disabled");
					callback=null;
					if(res.code==200){
						callback=function(){
						window.location.href=window.location.href;
						};
					}
					common_ops.alert(res.msg,callback);
				},
			});
		});
	},
	delete_img:function(){
		$(".upload_pic_wrap .del_image").unbind().click(function(){
			$(this).parent().remove();
		});
	},
};
$(document).ready(function(){
	brand_img_ops.init();
});